<?php

require_once("../../lib/config.php");
$configManager = new Config();

/*
  Uploadify v3.1.0
  Copyright (c) 2012 Reactive Apps, Ronnie Garcia
  Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 */

// Define a destination
$targetFolder = '/uploads'; // Relative to the root


if (!empty($_FILES)) {

    $tempFile = $_FILES['Filedata']['tmp_name'];
    //$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetPath = $configManager->getConfig('path.pdf');
//    $targetFile = rtrim($targetPath, '/') . '/' . $_FILES['Filedata']['name'];
    //这里不需要对上传文件进行重命名,因为没对上传文件数据据保存,只是磁盘保存文件,重命名的话,就没有文件的说明了,前台显示出来就是时间戳,不知道文件描述
    //但是不重命名的话,win下中文名会乱码,但是文档预览是ok的
    $ext = explode('.', $_FILES['Filedata']['name']);
    $extension = $ext[1];
    $save_name = time() . '.' . $extension;
    $targetFile = rtrim($targetPath, '/') . '/' . $save_name;

    $remark = $_REQUEST['remark'];
    $category = intval($_REQUEST['category']);
    $subject = intval($_REQUEST['subject']);
    $createat = time();
//    还是得写数据库啊
    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
    mysql_select_db('newhospital', $conn) or die(mysql_error());
    mysql_query("set names 'utf8'");
    $sql = "INSERT INTO think_archive (name, category, subject, remark, createat) VALUES (\"$save_name\", \"$category\", \"$subject\", \"$remark\", \"$createat\")";
//    $handle = fopen('request.txt', 'a');
//    $rebate_log = $subject;
//    fwrite($handle, $rebate_log . "\r");
    if (@mysql_query($sql, $conn)) {
        echo '1';
    } else {
        echo '文件数据保存失败,稍后再试!';
    }
    // Validate the file type
    $fileTypes = array('pdf');
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array($fileParts['extension'], $fileTypes)) {
        move_uploaded_file($tempFile, $targetFile);
        echo '1';
    } else {
        echo '文件格式不正确.';
    }
}
?>